<?php
namespace Common\Model;

class SettingModel extends XuecheModel {

    private static $OBJ;
    protected $tableName = 'setting';
    protected $fields = array(
        'name','value','disabled'
    );

    /**
     * 清空数据缓存
     *
     */
    public function ClearAllCache() {

    }

    //缓存设置
    protected $_useCacheType = 'file';
    protected $_catheTime = 0;

    public function Validate($data) {
        if(empty($data['name']) || empty($data['value'])){
            return FALSE;
        }
        return TRUE;
    }

    /**
     * 取值
     * @param type $name
     * @return boolean
     */
    public static function getValue($name,$val = ''){
        if(!isset(self::$OBJ)){
            self::$OBJ = new self();
        }
        if(empty($name))            return FALSE;
        $data = self::$OBJ->where("`name` = '{$name}'")->find();
        if(empty($data))            return FALSE;
        $data = json_decode($data['value'],true);
        if($val){
            return $data[$val];
        }
        return $data;
    }
    /**
     * 存值
     * @param type $data
     */
    public function saveValue($data){
        $this->Validate($data);
        $data['value'] = json_encode($data['value']);
        if($this->getValue($data['name']) === false){
            return $this->add($data);
        }else{
            return $this->where("`name` = '{$data['name']}'")->save($data);
        }
    }

}